package com.netty.demo.conntroller;

import com.alibaba.fastjson.JSONObject;
import com.fengzai.demo.facade.HelloFacade;
import com.fengzai.demo.facade.MessageFacade;
import com.fengzai.netty.rpc.annotation.RpcReference;
import com.fengzai.netty.rpc.cache.RpcIntegrationCache;
import com.fengzai.netty.rpc.cache.RpcRemoteServerCache;
import com.fengzai.netty.rpc.server.c.RpcClientHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Set;

/**
 * @PACKAGE_NAME: com.netty.demo.conntroller
 * @author: rhf
 * @ProjectName: netty
 * @DATE: 2021/11/6
 **/
@RestController
@RequestMapping("/test/")
public class TestController {
    @RpcReference
    private HelloFacade helloFacade;
    @RpcReference
    private MessageFacade messageFacade;

    @GetMapping("1")
    public String test1() {
        helloFacade.sayHello1();
        return "ok";
    }

    @GetMapping("2")
    public String test2() {
        helloFacade.sayHello2("我是中国人");
        return "ok";

    }

    @GetMapping("3")
    public String test3() {
        String s = helloFacade.sayHello3();
        System.out.println("test3返回：" + s);
        return "ok";

    }

    @GetMapping("4")
    public String test4() {
        String str = helloFacade.sayHello4("这是测试4");
        System.out.println("test4返回：" + str);
        return "ok";

    }

    @GetMapping("5")
    public String test5() {
        messageFacade.sayMsg1();
        return "ok";
    }

    @GetMapping("6")
    public String test6() {
        messageFacade.sayMsg2("i'm chinese");
        return "ok";

    }

    @GetMapping("7")
    public String test7() {
        String s = messageFacade.sayMsg3();
        System.out.println("test unit seven return：" + s);
        return "ok";

    }

    @GetMapping("8")
    public String test8() {
        String str = messageFacade.sayMsg4("this is test unit eight");
        System.out.println("test unit eight return：" + str);
        return "ok";
    }

    @PostMapping("9")
    public String test9(MultipartFile file) {
        String str = messageFacade.transferFile(file);
        System.out.println("test unit nine return：" + str);
        return "ok";
    }

    @GetMapping("allIntegration")
    public String allIntegration() {
        Map<String, Set<String>> all = RpcIntegrationCache.getAll();
        System.out.println(JSONObject.toJSONString(all));
        return JSONObject.toJSONString(all);
    }

    @GetMapping("allServer")
    public String allServer() {
        Map<String, RpcClientHandler> all = RpcRemoteServerCache.getAll();
        System.out.println(JSONObject.toJSONString(all));
        return JSONObject.toJSONString(all);
    }
}
